<?php

class HelpController extends Controller {

    public function actions() {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
        );
    }

    /**
     * Displays the contact page
     * 
     */
    public function actionContact() {
        $model = new ContactForm;
        if (isset($_POST['ContactForm'])) {
            $model->attributes = $_POST['ContactForm'];
            if ($model->validate()) {
                //$headers="From: {$model->email}\r\nReply-To: {$model->email}";
                //mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
                $post = new ContactModel;
                $post->name = $model->name;
                $post->email = $model->email;
                $post->subject = $model->subject;
                $post->message = $model->body;
                $post->status = 'unread';
                $post->contact_date = date('Y-m-d H:i:s');
                $body = "name : " . $post->name . "<br>";
                $body .= "email : " . $post->email . "<br>";
                $body .= "subject :" . $post->subject . "<br>";
                $body .= "message :" . $post->message . "<br>";
                SendMail::sendGmailAlertContact($body);
                $post->save();
                if ($post->id) {
                    Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
                    $this->refresh();
                } else {
                    echo "fail";
                }
            }
        }
        $this->render('contact', array('model' => $model));
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    public function actionSetLanguage($lang) {
        Yii::app()->session['lang'] = $lang;
        $this->redirect($_SERVER['HTTP_REFERER']);
    }

    public function actionUpload() {

        // import the class
        

//        $flgDelete = unlink(Yii::app()->getBasePath() . '/upload/photo/'.date('Ymd').'/test.jpg');
//        if($flgDelete)
//        {
//                echo "File Deleted";
//        }
//        else
//        {
//                echo "File can not delete";
//        }
                if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
                        //echo FileUpload::mainUpload($_FILES['photoimg']);exit;
                        Yii::import('application.components.imageupload.FileUpload');
                        echo '<img src='.FileUpload::mainUpload($_FILES['photoimg']).'>';exit;
                        exit;
                }
        $this->render('upload');
    }
    
     public function actionUp(){
     Yii::import("ext.EAjaxUpload.qqFileUploader");

                $folder='upload/';// folder for uploaded files
                $allowedExtensions = array("jpg");//array("jpg","jpeg","gif","exe","mov" and etc...
                $sizeLimit = 10 * 1024 * 1024;// maximum file size in bytes
                $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
                $result = $uploader->handleUpload($_SERVER['DOCUMENT_ROOT']. '/upload/');
                $result=htmlspecialchars(json_encode($result), ENT_NOQUOTES);
                echo $result;// it's array
     }   

    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */
}